package com.edu.dsai.controller;
import com.edu.dsai.dto.AIInput;
import com.edu.dsai.service.OllamaService;
import com.edu.dsai.utils.R;
import com.edu.dsai.utils.SSEServer;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.mvc.method.annotation.SseEmitter;

import java.util.List;


@RestController
@RequestMapping("/sse/")
@Slf4j
public class SSEController {
    @Resource
    private OllamaService ollamaService;
    /**
     * 连接sse服务的接口
     */
    @GetMapping(path = "connect",
            produces = {MediaType.TEXT_EVENT_STREAM_VALUE})
    public SseEmitter sseConnect(
            @RequestParam
            String userId) {
        return SSEServer.connect(userId);
    }
    @PostMapping("ai")
    public R<List<String>> ai(
            @RequestBody
            AIInput aiInput) {
        List<String> list = ollamaService.aiResponse(aiInput.getUserId(), aiInput.getMsg());
        return R.ok(list);
    }
}